package com.info3k.client.commands.location;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl3D;
import com.google.gwt.maps.client.control.MapTypeControl;
import com.google.gwt.maps.client.event.MapClickHandler;
import com.google.gwt.maps.client.event.MarkerDoubleClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.info3k.client.Info3K;
import com.info3k.client.components.GlassDialogBox;
import com.info3k.client.domain.Location;


public class TagLocationPositionsCommand extends GlassDialogBox implements Command {

	private MapWidget map = new MapWidget();
	
	private List<Marker> markers = new ArrayList<Marker>();
	
	public TagLocationPositionsCommand(final Location location){
		setText(Info3K.msgs.tagLocationPositions());
		map.setSize("400px", "300px");
		map.addControl(new LargeMapControl3D());
		map.setCurrentMapType(MapType.getSatelliteMap());
		map.addControl(new MapTypeControl());
		map.setCenter(LatLng.newInstance(43.850488,18.398074), 14);
		
		for (String string : location.getLatLong()) {
			String[] array = string.split("%");
			if(array.length==2){
				MarkerOptions options = MarkerOptions.newInstance(); 
				options.setDraggable(true);
				
				Marker marker = new Marker(LatLng.newInstance(Double.parseDouble(array[0]),Double.parseDouble(array[1])), options);
				marker.setDraggingEnabled(true);
				marker.addMarkerDoubleClickHandler(new MarkerDoubleClickHandler(){
					public void onDoubleClick(MarkerDoubleClickEvent event) {
							markers.remove(event.getSender());
							map.removeOverlay(event.getSender());
						} 
					}
				);
				map.addOverlay(marker);			
				map.setCenter(LatLng.newInstance(marker.getLatLng().getLatitude(), marker.getLatLng().getLongitude()), 14);
				markers.add(marker);
			}
		}
			
		content.add(map);
		
		Button okButton = new Button(Info3K.msgs.ok());
		footer.add(okButton);
		
		map.addMapClickHandler(new MapClickHandler(){
			public void onClick(MapClickEvent event) {
				if(event.getOverlay() instanceof Marker){
				} else {
					MarkerOptions options = MarkerOptions.newInstance(); 
					options.setDraggable(true);
					Marker marker = new Marker(event.getLatLng(), options);
					marker.addMarkerDoubleClickHandler(new MarkerDoubleClickHandler(){
						public void onDoubleClick(MarkerDoubleClickEvent event) {
								markers.remove(event.getSender());
								map.removeOverlay(event.getSender());
							} 
						}
					);
					
					marker.setDraggingEnabled(true);
					markers.add(marker);
					map.addOverlay(marker);
				}
			}
		});
		
		okButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				String[] list = new String[markers.size()];
				int i = 0;
				for (Marker marker : markers) {
					list[i] = marker.getLatLng().getLatitude() + "%" + marker.getLatLng().getLongitude();
					i++;
				}
				location.setLatLong(list);
				hide();
			}
		});
	}

	public void execute() {
		center();
	}
}